Ext.define("core.system.controller.SysController", {
    extend: "Ext.app.Controller",

    init: function () {
        var self = this;
        this.control({
            "sysGrid": {
                itemdblclick: function (_grid, record, item, index, e, eOpts) {
                    var win = Ext.widget("sysWindow");
                    var form = win.down("form");
                    var item = form.getForm().findField("userName");
                    item.setReadOnly(true);
                    //把选择的数据加载到form中去
                    form.loadRecord(record);//加载数据
                    var btn = form.down("button[ref=reset]");
                    form.down("button[ref=save]").enable();
                    btn.disable();
                    win.show();
                }

            },
            /**
             * 添加用户form的保存按钮
             */
            "sysWindow button[ref=save]": {
                click: function (btn) {
                    //1获得form
                    var _form = btn.ownerCt.ownerCt;
                    //2.把数据保存到数据库中去
                    _form.submit({
                        clientValidation: true,
                        waitMsg: '正在进行处理,请稍后...',
                        url: "userInfo/updatePassword",
                        method: 'POST',
                        success: function (form, action) {
                            var resObj = Ext.decode(action.response.responseText);
                            if (resObj.success) {
                                Ext.getCmp("sysWindow").close();
                                var _grid = Ext.widget("sysGrid");
                                var store = _grid.getStore();
                                store.load();
                                Ext.Msg.alert("提示", resObj.msg);
                            } else {
                                Ext.Msg.alert("提示", resObj.msg);
                            }
                        },
                        failure: function (form, action) {
                            Ext.Msg.alert("错误", "表单数据处理有误，无法请求到后台!");
                        }
                    });

                }
            },

            //重置事件
            "sysWindow button[ref=reset]": {
                click: function (btn) {
                    var form = btn.ownerCt.ownerCt;
                    form.getForm().reset();
                }
            },
            /**
             * 添加用户form的返回按钮
             */
            "sysWindow button[ref=return]": {
                click: function (btn) {
                    Ext.getCmp("sysWindow").close();
                }
            }
        });
    },
    views: [
        "core.system.view.SysLayout",
        "core.system.view.SysGrid",
        "core.system.view.SysWindow"
    ],
    stores: ["core.system.store.SysStore"],
    models: ["core.user.model.UserModel"]
});